/*
 * @Author: your name
 * @Date: 2020-05-15 16:18:43
 * @LastEditTime: 2020-05-21 14:26:45
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\models\m_user_behavior.js
 */ 


const Sequelize = require('sequelize')



module.exports = (sequelize, DataTypes ) =>{

    /**
     * @author: xgq
     * @description: 检查该用户是否已经点赞过该图片 
     * @param {type} 
     * @return: 
     */    
    class userBehavior extends Sequelize.Model{
        static async checkUserLike(pars){
            var user_id = pars.uid;
            var img_id = pars.image_id
            var behavior_type=pars.behavior_type
            var result = await this.findAll({where:{user_id,img_id,behavior_type}})
            if(result.length>0){
                return true
            }else{
                return false;
            }
        }
        // 根据用户id查出收藏过的图片
        static async getImageIdByUid(uid){
           var list=await this.findAll({attributes:['img_id'],where:{user_id:uid,behavior_type:0},raw:true})
           return list;
       }
    }


    userBehavior.init({
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        img_id:{type:DataTypes.INTEGER},
        user_id:{type:DataTypes.INTEGER},
        behavior_type:{type:DataTypes.INTEGER },
        create_time: { type: DataTypes.INTEGER }
      }, {
        sequelize,
        modelName: 'user_behavior',
        freezeTableName: true,
        timestamps: false
      });

      return userBehavior;

}

